/*
 * (C) 2014-2015 Alibaba Group Holding Limited
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *
 *     __  ___   ______        ___       __          ___
 *    |  |/  /  /  __  \      /   \     |  |        /   \
 *    |  '  /  |  |  |  |    /  ^  \    |  |       /  ^  \
 *    |    <   |  |  |  |   /  /_\  \   |  |      /  /_\  \
 *    |  .  \  |  `--'  |  /  _____  \  |  `----./  _____  \
 *    |__|\__\  \______/  /__/     \__\ |_______/__/     \__\
 *
 *
 *
 */
//
// Created by KOALA TEAM on 12/19/14.
//

#import "KLAppDelegate.h"
#import "KLIAppViewModel.h"
#import "KLNotification.h"
#import "KLLog.h"
#import "KLAppViewModel.h"



@interface KLAppDelegate ()

@property(nonatomic,strong)NSMutableDictionary *notif_cmds;


@end

@implementation KLAppDelegate

- (id<KLIAppViewModel>)createAppViewModel{
    NSAssert(0, @"should override createAppViewModel");
    return nil;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.appViewModel = [self createAppViewModel];
    [self.appViewModel configure];
    
    //detect update
    [[self.appViewModel.updateViewModel checkUpdateCmd]execute:nil];
    [[self.appViewModel.updateViewModel hasNewVersion] subscribeNext:^(id x) {
        [self showNewUpdate:x];
    }];

    
    //push operations
    //regiseter
    [[self.appViewModel.pushViewModel registerPush]execute:nil];
    //bind
    [self.appViewModel registerCmd:[[RACCommand alloc]initWithEnabled:nil signalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            RACDisposable *disposable = [[[self.appViewModel.pushViewModel bindUser:input] execute:input] subscribe:subscriber];
            return disposable;
        }];
    }]   forNotification:kKLDidLoginNotification];
    
    //unbind
    [self.appViewModel registerCmd:[[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            RACDisposable *disposable = [[[self.appViewModel.pushViewModel  unbindUser]execute:input]subscribe:subscriber];
            return disposable;
        }];
    }] forNotification:kKLDidLogoutNotification];
    
    
    
    //login
    [self.appViewModel registerCmd:[[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [self showLogin];
            return nil;
        }];
    }] forNotification:kKLNeedLoginNotification];
    
    
    
    return YES;
}


- (void)showNewUpdate:(KLUpdateModel*)model{
    NSAssert(0, @"should override showNewUpdate:");
}

- (void)showLogin{
    //
}
- (void)applicationWillResignActive:(UIApplication *)application {

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[self.appViewModel.updateViewModel checkUpdateCmd]execute:nil];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
}

- (void)applicationWillTerminate:(UIApplication *)application {

}


#pragma mark -
#pragma mark remote push 
- (void)recvedRemoteNotification:(KLModel*)model{
    NSAssert(0, @"should override recvedRemoteNotification:");
}

- (void)pareseRemoteNotificationFailed:(NSError*)error{
    NSAssert(0, @"should override pareseRemoteNotificationFailed:");
}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0){
    [[[self.appViewModel.pushViewModel uploadDeviceToken:deviceToken] execute:nil] subscribeError:^(NSError *error) {
        KLLogError(@"recv remote notification:%@",error);
    }];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0){
    KLLogError(@"register push:%@",error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_AVAILABLE_IOS(3_0){
    [[[self.appViewModel.pushViewModel recvedRemoteNotificaton:userInfo] execute:userInfo] subscribeNext:^(id x) {
        [self recvedRemoteNotification:x];
    } error:^(NSError *error) {
        [self pareseRemoteNotificationFailed:error];
    }];
}


- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0){
    
    
    
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0){
    
}

#pragma mark open url
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [self.appViewModel openUrl:url fromApplication:application];
}


@end
